package com.groupsix.service.impl;

import com.groupsix.mapper.*;
import com.groupsix.pojo.SysCar;
import com.groupsix.pojo.SysCityDto;
import com.groupsix.pojo.SysOrder;
import com.groupsix.pojo.SysUser;
import com.groupsix.service.OrderService;
import com.groupsix.vo.OrderInfoVo;
import com.groupsix.vo.OrderListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    private double basePrice = 50;
    static double shouwxufei =35;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private SysCityMapper cityMapper;
    @Resource
    private SysCarMapper  carMapper;
    @Resource
    private SysUserMapper  userMapper;
    @Override
    public OrderInfoVo searchOrderInfo(int getid, int backid, int cid) {
        OrderInfoVo infoVo = new OrderInfoVo();
        SysCityDto cityDto1 = cityMapper.findCityNameById(getid);

//        具体的取车地址（城市+位置）
        String getName = cityDto1.getCityName()+cityDto1.getName();
        infoVo.setGetcitys(getName);//取车地址
        SysCityDto cityDto2 = cityMapper.findCityNameById(backid);
        String backName = cityDto2.getCityName()+cityDto2.getName();
        infoVo.setBackcitys(backName);//还车地址
        SysCar car = carMapper.findCarById(cid);
        infoVo.setXcCarinfo(car);//所选车辆

        return infoVo;
    }

    @Override
    public boolean addOrder(int getid, int backid, int cid, String tel) {

        SysOrder orderInfo =new SysOrder();
        orderInfo.setCid(cid);
        orderInfo.setBackid(backid);
        orderInfo.setGetid(getid);
//        根据tel查询用户信息
        SysUser sysUser = userMapper.loginByTelAndPassword(tel);
        orderInfo.setUid(sysUser.getId());
//        获取租车信息
        SysCar car = carMapper.findCarById(cid);
        double oprice = car.getPrice()+ basePrice +shouwxufei;
        orderInfo.setOprice(oprice);
        orderInfo.setStatus("已预定");
        int i = orderMapper.addOrder(orderInfo);
        if(i>-1){
            return true;
        }
        return false;
    }

    @Override
    public List<OrderListVo> findMyOrders(int uid) {
        List<OrderListVo> orderListVos = orderMapper.myOrders(uid);
        return orderListVos;
    }

    @Override
    public int delet(int id) {
        int delet = orderMapper.delet(id);
        return delet;
    }
}
